package westh.ilib.util;
/**
 * Created resource defined in OAI to resource defined in CDOI
 * @author Administrator
 *
 */
import org.apache.commons.logging.Log;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ConvertRscToCDOIRsc {
	
	public static JSONObject convertRscToCdoiRsc(JSONObject oaiJson,Log log){
		 JSONObject jsonCdoi=null;
		 String rscTitle="NULL";
		 String rscFirstAuthorName="NULL";
		 String rscUrl="NULL";
		  try{
		  rscTitle=oaiJson.getString("rscTitle");
		  
		  String rscFirstAuthorJson=oaiJson.getString("rscFirstAuthor");
		  
		  if(!rscFirstAuthorJson.equals("")){
			  JSONObject firstAuthor=JSONObject.fromObject(rscFirstAuthorJson);
			  rscFirstAuthorName=firstAuthor.getString("name");
		  }
		  
		  //String rscPublishDate=oaiJson.getString("rscPublishDate");
		  rscUrl=oaiJson.getString("rscUrl");
		  
		
		
		 // String rId = "012f717f8dde4129a95837bf153de5a0"+strId;
		  //形成json进行封装 
		  jsonCdoi = new JSONObject(); 
		  //jsonCdoi.put("WESTDO_HANDLE",rId);                                    //唯一标识
		  jsonCdoi.put("CDOI_AGENCY_ID", "012f717f8dde4129a95837bf153de5a0");   //注册机构编码
		  jsonCdoi.put("DC_LANGUAGE", "c7d2ec860cee4b03ba845da0233ed977");      //语种标识符
		  jsonCdoi.put("CDOI_CUSTOMER_ID", "f41825a60cf44e83b83cbd9114220f29"); //用户标识符
		  jsonCdoi.put("DC_TYPE", "27278cfe6e1c429aa2197b3ff34a11ab");          //类型标识符
		  jsonCdoi.put("DC_TITLE",rscTitle );                                   //题名
		  jsonCdoi.put("DC_CREATOR",rscFirstAuthorName);                                //创建者
		  jsonCdoi.put("DC_SUBJECT", "");                                       //主题
		  jsonCdoi.put("DC_DESCRIPTION", "");                           //描述
		  jsonCdoi.put("DC_PUBLISHER", "");                                     //出版者
		  jsonCdoi.put("DC_CONTRIBUTOR", "");                                   //其他责任者
		  jsonCdoi.put("DC_DATE", "");                                  //日期
		  jsonCdoi.put("DC_FORMAT", "");                                        //格式
		  jsonCdoi.put("DC_IDENTIFIER", "");                                    //标识符
		  jsonCdoi.put("DC_SOURCE", "");                                        //来源
		  jsonCdoi.put("DC_RELATION", "");                                      //关联
		  jsonCdoi.put("DC_COVERAGE", "");                                      //覆盖范围                         
		  jsonCdoi.put("DC_RIGHTS", "");                                        //权限
		  jsonCdoi.put("CDOI_URL_SOURCE", rscUrl);                                  //资源URL
		  jsonCdoi.put("CDOI_STATUS", "1");                                     //资源注册状态，2审核通过
		  jsonCdoi.put("CDOI_NOTE", "");                                        //备注
		  }catch(Exception e){
			  e.printStackTrace();
			  log.debug("Error in converting rsc to cdoi rsc:"+oaiJson);
			  log.debug("Error in converting.");
		  }
		return jsonCdoi;
	}	
	public static String convertRscListToCdoi(JSONArray arrayJson,Log log){
		JSONArray cdoiArray=new JSONArray();
		for(int i=0;i<arrayJson.size();i++){
			JSONObject json=arrayJson.getJSONObject(i);
			JSONObject jsonCdoi=convertRscToCdoiRsc(json,log);
			cdoiArray.add(jsonCdoi);
		}
		return cdoiArray.toString();
	}
	
	public static String addRscIds(JSONArray arrayJson, JSONArray idArray){
		JSONArray resultArray=new JSONArray();
		for(int i=0;i<arrayJson.size();i++){
			String id=idArray.getString(i);
		    JSONObject json=arrayJson.getJSONObject(i);
		    json.put("rscId", id);
			resultArray.add(json);
		}
		return resultArray.toString();
	}
	
	public static String addAuthorIds(JSONArray arrayJson, JSONArray idArray){
		JSONArray resultArray=new JSONArray();
		for(int i=0;i<arrayJson.size();i++){
			String id=idArray.getString(i);
		    JSONObject json=arrayJson.getJSONObject(i);
		    json.put("uid", id);
			resultArray.add(json);
		}
		return resultArray.toString();
	}

}
